/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class ShapeParser {
    
    public ArrayList<Shape> parseFile(String filePath) throws IOException {
        ArrayList<Shape> shapes = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        
        String line;
        while ((line = reader.readLine()) != null) {
            // Skip empty lines and comments
            line = line.trim();
            if (line.isEmpty() || line.startsWith("#")) {
                continue;
            }
            
            try {
                Shape shape = parseShape(line);
                if (shape != null) {
                    shapes.add(shape);
                }
            } catch (IllegalArgumentException e) {
                System.err.println("Skipping invalid shape definition: " + line);
            }
        }
        
        reader.close();
        return shapes;
    }
    
    private Shape parseShape(String shapeDef) {
        String[] parts = shapeDef.split("\\s+");
        if (parts.length < 1) {
            return null;
        }
        
        String type = parts[0].toLowerCase();
        try {
            switch (type) {
                case "circle":
                    if (parts.length != 5) throw new IllegalArgumentException("Circle requires 4 parameters");
                    int x = Integer.parseInt(parts[1]);
                    int y = Integer.parseInt(parts[2]);
                    int radius = Integer.parseInt(parts[3]);
                    
                    Circle circle = new Circle(x, y, radius);
                    
                    return circle;
                    
                case "square":
                    if (parts.length != 5) throw new IllegalArgumentException("Square requires 4 parameters");
                    x = Integer.parseInt(parts[1]);
                    y = Integer.parseInt(parts[2]);
                    int width = Integer.parseInt(parts[3]);
                    
                    Square square = new Square(x, y, width);
                   
                    return square;
                    
                case "rectangle":
                    if (parts.length != 6) throw new IllegalArgumentException("Rectangle requires 5 parameters");
                    x = Integer.parseInt(parts[1]);
                    y = Integer.parseInt(parts[2]);
                    width = Integer.parseInt(parts[3]);
                    int height = Integer.parseInt(parts[4]);
                 
                    Rectangle rectangle = new Rectangle(x, y, width, height);
              
                    return rectangle;
                    
                case "triangle":
                    if (parts.length != 6) throw new IllegalArgumentException("Triangle requires 5 parameters");
                    x = Integer.parseInt(parts[1]);
                    y = Integer.parseInt(parts[2]);
                    int base = Integer.parseInt(parts[3]);
                    height = Integer.parseInt(parts[4]);
              
                    Triangle triangle = new Triangle(x, y, base, height);
             
                    return triangle;
                    
                default:
                    throw new IllegalArgumentException("Unknown shape type: " + type);
            }
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Invalid number format in shape definition");
        }
    }
    

    
    public HashMap<String, Integer> countByType(ArrayList<Shape> shapes) {
        HashMap<String, Integer> counts = new HashMap<>();
        counts.put("Circle", 0);
        counts.put("Square", 0);
        counts.put("Rectangle", 0);
        counts.put("Triangle", 0);
        
        for (Shape shape : shapes) {
            if (shape instanceof Circle) {
                counts.put("Circle", counts.get("Circle") + 1);
            } else if (shape instanceof Square) {
                counts.put("Square", counts.get("Square") + 1);
            } else if (shape instanceof Rectangle) {
                counts.put("Rectangle", counts.get("Rectangle") + 1);
            } else if (shape instanceof Triangle) {
                counts.put("Triangle", counts.get("Triangle") + 1);
            }
        }
        
        return counts;
    }
    
    public double totalArea(ArrayList<Shape> shapes) {
        double total = 0;
        for (Shape shape : shapes) {
            total += shape.getMass(); // Using mass as area for these shapes
        }
        return total;
    }
}
